home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / pcomm / Source / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  1.0 KB  |  57 lines

  1. /*
  2.  * Parse the command line and return option flags and arguments
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "config.h"
  7.  
  8. int optind = 1;
  9. char *optarg;
  10.  
  11. int
  12. getopt(argc, argv, opts)
  13. int argc;
  14. char *argv[];
  15. char *opts;
  16. {
  17.     static int sp = 1;
  18.     int c, strcmp();
  19.     char *cp, *strchr();
  20.  
  21.     if (sp == 1) {
  22.         if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
  23.             return(EOF);
  24.         else if (strcmp(argv[optind], "--") == 0) {
  25.             optind++;
  26.             return(EOF);
  27.         }
  28.     }
  29.     c = argv[optind][sp];
  30.     if (c == ':' || (cp=strchr(opts, c)) == NULL) {
  31.         fprintf(stderr, "%s: illegal option \"%c\"\n", argv[0], c);
  32.         if (argv[optind][++sp] == '\0') {
  33.             optind++;
  34.             sp = 1;
  35.         }
  36.         return('?');
  37.     }
  38.     if (*++cp == ':') {
  39.         if (argv[optind][sp+1] != '\0')
  40.             optarg = &argv[optind++][sp+1];
  41.         else if (++optind >= argc) {
  42.             fprintf(stderr, "%s: option \"%c\" requires an argument\n", argv[0], c);
  43.             sp = 1;
  44.             return('?');
  45.         } else
  46.             optarg = argv[optind++];
  47.         sp = 1;
  48.     } else {
  49.         if (argv[optind][++sp] == '\0') {
  50.             sp = 1;
  51.             optind++;
  52.         }
  53.         optarg = NULL;
  54.     }
  55.     return(c);
  56. }
  57.